package com.fanh.controller;

import com.fanh.common.api.CommonPage;
import com.fanh.common.api.CommonResult;
import com.fanh.dto.GuideCourseResult;
import com.fanh.model.GuideComment;
import com.fanh.model.GuideCourse;
import com.fanh.model.GuideLesson;
import com.fanh.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "ReadIngManagerController", description = "阅读管理")
@RequestMapping("/")
public class ReadIngManagerController {

    @Autowired
    private GuideReadHistoryService guideReadHistoryService;

    @Autowired
    private UserService userService;

    @Autowired
    private GuidePayService guidePayService;
    @Autowired
    private  GuideCourseService guideCourseService;
    @Autowired
    private GuidelessonService guidelessonService;

    @ApiOperation("记录已读文章")
    @RequestMapping(value = "/a/guide/s/course/read", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult read(@ApiParam(value = "lessonId", required = true) @RequestParam("lessonId")String lessonId) {
        String userId= userService.getCurrentSysUser().getId();
        int count =guidelessonService.updateRead(userId,lessonId);
        if(count>0){
            return  CommonResult.success(count);
        }
        return CommonResult.failed("操作失败");
    }
    @ApiOperation("查询课程购买的课次")
    @RequestMapping(value = "a/guide/s/lesson/buies", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult buies(@ApiParam(value = "courseId", required = true) @RequestParam("courseId")String courseId) {
        String userId= userService.getCurrentSysUser().getId();
       List list = guidelessonService.getPayLesson(userId,courseId);
       return CommonResult.success(list);
    }
    @ApiOperation("检测课次是否学习过")
    @RequestMapping(value = "a/guide/s/lesson/checkstudied", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult checkstudied(@ApiParam(value = "lessonId", required = true) @RequestParam("lessonId")String lessonId) {
        String userId= userService.getCurrentSysUser().getId();
        boolean result =  guidelessonService.checkLessonStudy(userId,lessonId);
        return CommonResult.success(result);
    }
    @ApiOperation("检测课次是否免费")
    @RequestMapping(value = "a/guide/s/course/paycheck", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult paycheck(@ApiParam(value = "courseId", required = true) @RequestParam("courseId")String courseId) {
        String userId= userService.getCurrentSysUser().getId();
        Map<String, Object> result = guideCourseService.paycheck(userId,courseId);
        return CommonResult.success(result);
    }
    @ApiOperation("学单列表-已订阅")
    @RequestMapping(value = "/a/guide/s/pay/course", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult course(@ApiParam(value = "type", required = false) @RequestParam("type")Integer type,
                       @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                       @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        String userId= userService.getCurrentSysUser().getId();
       List<GuideCourseResult> results= guidePayService.payCourseReading(userId,type,pageNo,pageSize);
       return CommonResult.success(CommonPage.restPage(results));
    }
    @ApiOperation("观看历史")
    @RequestMapping(value = "/a/guide/s/course/readHistory", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult readHistory(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        String userId= userService.getCurrentSysUser().getId();
        List<GuideCourse> results= guideCourseService.readHistory(userId,pageNo,pageSize);
        return CommonResult.success(CommonPage.restPage(results));
    }
    @ApiOperation("订阅操作")
    @RequestMapping(value = "/a/guide/s/pay/freeSubscribe", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult freeSubscribe(@ApiParam(value = "courseId", required = true) @RequestParam("courseId")String courseId) {
        String userId= userService.getCurrentSysUser().getId();
       int count = guidePayService.freeSubject(userId,courseId);
       if(count>0){
           return  CommonResult.success(count);
       }
       return CommonResult.failed("操作失败");
    }

    @ApiOperation("课程学习历史记录")
    @RequestMapping(value = "a/guide/s/lesson/studies", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult studies(@ApiParam(value = "courseId", required = true) @RequestParam("courseId")String courseId,
                        @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                        @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        String userId= userService.getCurrentSysUser().getId();
        List lessons = guideReadHistoryService.guideReadHistory(userId,courseId,pageNo,pageSize);

        return CommonResult.success(CommonPage.restPage(lessons));

    }


}
